package main

import "fmt"

func main() {

	var m1 map[string]int //必须初始化才能使用
	m1 = make(map[string]int)
	m1["one"] = 1
	m1["two"] = 2
	m1["three"] = 3
	fmt.Println(m1)

	value, ok := m1["one"]
	if ok {
		fmt.Println("value:", value)
	} else {
		fmt.Println("key not found")
	}
	for key, value := range m1 {
		fmt.Printf("key: %s, value: %d\n", key, value)
	}
	//删除元素 delete 删除不存在的key无影响
	delete(m1, "one")
	fmt.Println(m1)

	//map 和slice组合 切片和map都要初始化
	m2 := make([]map[string]int, 10, 10)
	m2[0] = make(map[string]int)
	m2[0]["one"] = 1
	m2[0]["two"] = 2
	m2[0]["three"] = 3
	fmt.Println(m2)
	//值为切片类型的map
	m3 := make(map[string][]int)
	m3["one"] = []int{1, 2, 3}
	m3["two"] = []int{4, 5, 6}
	fmt.Println(m3)
}
